
const APP = {
    //检查存储权限
    hasStoragePermission: function () {
        // 获取应用的PackageManager实例
        var pm = context.getPackageManager();
        // 获取应用的包名
        var packageName = context.getPackageName();
        // 检查权限
        var permission = android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
        var result = pm.checkPermission(permission, packageName);

        // 如果结果等于PackageManager.PERMISSION_GRANTED，则表示有权限
        return result == android.content.pm.PackageManager.PERMISSION_GRANTED;
    },
    //打开应用
    launchApp: function (packageName) {
        // 通过包名打开应用
        var appInfo = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (appInfo != null) {
            context.startActivity(appInfo);
            return true;
        } else {
            // toast("找不到应用: " + packageName);
            return true;
        }
    },
    //按坐标点击
    click: function (x, y, delay) {
        myUi.setTouchable(false)
        sleep(500)
        click(x, y);
        sleep(500)
        myUi.setTouchable(true)
        if(delay == true){
            APP.randomSleep()
            return
        }
        sleep(delay * 1000);
    },
    // 按钮点击
    clickByBtn: function (btn, delay) {
        if(!btn){
            return
        }
        myUi.setTouchable(false)
        sleep(500)
        let clickable = btn.click();
        if (!clickable) {
            let space = btn.bounds()
            click(space.centerX(), space.centerY())
        }
        sleep(500)
        myUi.setTouchable(true)
        if(delay == true){
            APP.randomSleep()
            return
        }
        sleep(delay * 1000);
    },
    //打开个人中心并获取小红书号
    getAccount() {
        id('igz').className('android.widget.TextView').text('我').waitFor();
        if (!id('igz').className('android.widget.TextView').exists()) {
            back();
        }
        if (!id('igz').className('android.widget.TextView').exists()) {
            back();
        }
        App.click(958, 2227, 1, '点击我的，获取小红书号'); //点击我的
        id('gxr').waitFor();
        var text = id('gxr').findOne().text();
        console.log(text);
        //提示草稿箱关闭
        if (className("android.widget.ImageView").depth(6).exists()) {
            className("android.widget.ImageView").click();
        }
    },
    HttpRequest: function () {
        var url = 'https://zfg.isimi.net/app/code/getCodeData';
        var param = {
            a: 1,
            b: 2,
        };

        let response = http.postJson(url, param);
        if (response.statusCode == 200) {
            let jsonData = response.body.json();
            return jsonData;
        } else {
            console.log('请求失败，状态码', response.statusCode);
            return false;
        }
    },
    clickBtn(btn,position,time){
        if (btn) {
            APP.click(btn.bounds().centerX(), btn.bounds().centerY(), time)
        } else {
            APP.click(position.x, position.y, time)
        }
    },
    getTask: function () {
        var jsonData = { type: 'send_post' };
        return jsonData;
    },
    openBtn(index, config) {
        let swithcConfig = [
            btnConfig.home.homeBtn,
            btnConfig.home.homeHotBtn,
            btnConfig.home.homePostBtn,
            btnConfig.home.homeMessageBtn,
            btnConfig.home.homeMyBtn,
        ]
        let configBtn = config || swithcConfig[index].options
        var btn = configBtn.desc ? depth(configBtn.depth).id(configBtn.id).descContains(configBtn.desc).findOne(2000) : depth(configBtn.depth).id(configBtn.id).findOne(2000)
        if(configBtn.text){
            btn = btn.findOne(textContains(configBtn.text))
        }
        if (btn) {
            let clickable = btn.click()
            if (!clickable) {
                let space = btn.bounds()
                click(space.centerX(), space.centerY())
            }
        }
    },
    randomSleep(min,max){
       let time = max ? (Math.floor(Math.random() * (max - min + 1)) + min)*1000 : (Math.floor(Math.random() * (4)) + 3) * 1000
       console.log('睡：' + time + 'ms');
       sleep(time)
    }
};
module.exports = APP